package cn.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import cn.hr.pojo.Post;
import cn.hr.pojo.SysUser;
import cn.hr.service.DictionaryService;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hr.service.PostService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 职位控制器
 * @author MO
 *
 */
@Controller
@RequestMapping("/sys/post")
public class PostController {
	private Logger logger = LoggerFactory.getLogger(PostController.class);
	@Resource
	private PostService postService;
	private String meagess;

	/**
	 * 查询
	 * @return
	 */
	@RequestMapping("/showList")
	public String list(@RequestParam(value="name",required = false)String name,
					   @RequestParam(value="oId",required = false)String oId,
					   @RequestParam(value="pageIndex",required = false)String pageIndex,
					   @RequestParam(value = "isStart",required = false)String isStart,
					   @RequestParam(value = "show",required = false)String show,
					   Model model){
		Map<String,Object> map = new HashMap<>();
		Integer index = 1;
		if (pageIndex!=null&&pageIndex!="") index = Integer.parseInt(pageIndex);
		if (name!=null&&name!="")map.put("name",name);model.addAttribute("name",name);
		if (isStart!=null&&isStart!="")map.put("isStart",isStart);model.addAttribute("isStart",isStart);
		if (oId!=null&&oId!="")map.put("oId",Integer.parseInt(oId));model.addAttribute("oId",oId);
		try {
			PageInfo<Map> pageInfo = postService.findListByCondition(map,index);
			model.addAttribute("pageInfo",pageInfo);
			model.addAttribute("show",show);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "post/showList";
	}

	@RequestMapping("/list")
	public String showList(){
		return "post/postList";
	}

	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Model model){
		model.addAttribute("attr","add");
		Post post = new Post();
		post.setIsStart(0);
		model.addAttribute("post",post);
	return "post/AU";
	}

	/**
	 * 添加
	 * @param post
	 * @param session
	 * @return
	 */
	@RequestMapping("/addSave")
	public String addSave(Post post, HttpSession session){
		post.setCreatedById(((SysUser)(session.getAttribute(Constants.USER_SESSION))).getId());
		post.setCreatedDate(new Date());
		if(post.getIsStart()==null){
			post.setIsStart(0);
		}else{
			post.setIsStart(1);
		}
		try {
			if (postService.insert(post)){
				meagess = "redirect:list";
			}else{
				meagess = "redirect:add";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return meagess;
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(@RequestParam(value="id")String id){
		try {
			if (postService.delete(Integer.parseInt(id))){
				meagess = "true";
			}else{
				meagess = "false";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return meagess;
	}

	/**
	 * 跳转到修改页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/update")
	public String update(@RequestParam(value = "id")String id,Model model){
		try {
			Post post = postService.get(Integer.parseInt(id));
			model.addAttribute("attr","update");
			model.addAttribute("oId",post.getoId());
			model.addAttribute("sort",post.getSort());
			model.addAttribute("post",post);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "post/AU";
	}

	/**
	 * 修改
	 * @param post
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateSave")
	public String updateSave(Post post,HttpSession session){
		post.setModifyById(((SysUser)(session.getAttribute(Constants.USER_SESSION))).getId());
		post.setModifyDate(new Date());
		if(post.getIsStart()==null){
			post.setIsStart(0);
		}else{
			post.setIsStart(1);
		}
		try {
			if (postService.update(post)){
				meagess = "redirect:list";
			}else{
				meagess = "redirect:update?id="+post.getId().toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  meagess;
	}

	@RequestMapping("/select")
	public String selectById(@RequestParam(value = "id")String id,Model model){
		Post post = null;
		try {
			post = postService.get(Integer.parseInt(id));
			model.addAttribute("post",post);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "";
	}
}
